package com.juku2024.juku.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.TextView
import com.juku2024.juku.R
import com.juku2024.juku.tools.DisplayUtil

class CommonDialog(context: Context, style: Int = R.style.CommonDialogStyle): Dialog(context, style) {

    private var title: String? = null
    private var msg: String? = null
    private var btnConfirmText: String? = null
    private var onConfirmClickListener: View.OnClickListener? = null
    private var btnCancelStr: String? = null
    private var oncCancelClickListener: View.OnClickListener? = null

    private var isInitView = false

    private lateinit var btnCancel: Button
    private lateinit var btnConfirm: Button
    private lateinit var tvDialogMsg: TextView
    private lateinit var tvDialogTitle: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_common)

        btnCancel = findViewById(R.id.btn_cancel_dialog_common)
        btnConfirm = findViewById(R.id.btn_confirm_dialog_common)
        tvDialogMsg = findViewById(R.id.tv_dialog_msg)
        tvDialogTitle = findViewById(R.id.tv_dialog_title)

        btnConfirm.requestFocusFromTouch()

        isInitView = true

        initView()
    }

    private fun initView() {
        if (!isInitView) return

        if (!TextUtils.isEmpty(title)) {
            tvDialogTitle.text = title
        }

        tvDialogMsg.text = msg

        if (!TextUtils.isEmpty(btnConfirmText)) {
            btnConfirm.text = btnConfirmText
        }

        if (!TextUtils.isEmpty(btnCancelStr)) {
            btnCancel.text = btnCancelStr
        }

        if (onConfirmClickListener == null) {
            btnConfirm.visibility = View.GONE
        } else {
            btnConfirm.setOnClickListener {
                onConfirmClickListener?.onClick(it)
                dismiss()
            }
        }

        if (oncCancelClickListener == null) {
            btnCancel.visibility = View.GONE
        } else {
            btnCancel.setOnClickListener {
                oncCancelClickListener?.onClick(it)
                dismiss()
            }

        }
    }


    fun showDialog(
                   msg: String,
                   btnConfirmText: String?,
                   onConfirmClickListener: View.OnClickListener?,
                   btnCancelStr: String? = null,
                   oncCancelClickListener: View.OnClickListener? = null){
        this.btnCancelStr = btnCancelStr
        this.msg = msg
        this.btnConfirmText = btnConfirmText
        this.onConfirmClickListener = onConfirmClickListener
        this.oncCancelClickListener = oncCancelClickListener

        initView()
    }


    fun showDialog(title: String,
        msg: String,
                   btnConfirmText: String?,
                   onConfirmClickListener: View.OnClickListener?,
                   btnCancelStr: String? = null,
                   oncCancelClickListener: View.OnClickListener? = null){
        this.btnCancelStr = btnCancelStr
        this.title = title
        this.msg = msg
        this.btnConfirmText = btnConfirmText
        this.onConfirmClickListener = onConfirmClickListener
        this.oncCancelClickListener = oncCancelClickListener

        initView()
    }


    override fun onStart() {
        super.onStart()
        val dialogWight = DisplayUtil.dp2px(context, 300f)
        window!!.setLayout(dialogWight, WindowManager.LayoutParams.WRAP_CONTENT)
        setCanceledOnTouchOutside(false) //点击边际可消失
    }



}